<?php
/**
 * Created by PhpStorm.
 * User: KaZeline
 * Date: 15-6-5
 * Time: 上午9:57
 * alipay/ali_order 里面复制出来的 以防不测
 */
// 转分销分润
/**
 * @param $s    soap实例
 * @param $ordern 订单号
 * @param $aid 供应商id
 * @param $mid 分销商id
 * @param $total_fee 金额
 * @param $orders 订单
 * @param $to_pft
 * @param int $p_type /*金额类型 0帐号资金 1支付宝 2供应商处可用资金 3供应商信用额度设置 4财付通 5银联
 * @return int|string
 */
function fenRun($s, $ordern, $aid, $mid, $total_fee, $orders, $to_pft, $p_type = 1,$memo='')
{
    $aid = !$aid ? (int)$orders['mainOrder']->UUaid : $aid;
    $mid = !$mid ? (int)$orders['mainOrder']->UUmid : $mid;

    $add = 0;
    $cut = 1;
    // 非自供产品获取结算价
    if($aid!=$mid){
        $total_money = 0;
        $pid = (string)$orders['mainOrder']->UUpid;// 产品 id
        $begintime = (string)$orders['mainOrder']->UUbegintime;// 游玩时间价格
        $account = (string)$orders['mainOrder']->UUfromt;// 游玩时间价格
        $ret = $s->Dynamic_Price_And_Storage($account,$pid,$begintime,1,0,0,$aid);
        if($ret==105||$ret==1065){
            $total_money = $total_fee;
        }else{
            $obj = simplexml_load_string($ret);
            $total_money = (string)$obj->Rec->UUsprice * (int)$orders['mainOrder']->UUtnum;
            // write_logs("UUsprice".(string)$obj->Rec->UUsprice,"debug.txt");
        }
        if(count($orders['childOrder']) && $orders['mainOrder']->UUp_type!='F'){
            foreach($orders['childOrder'] as $key=>$child){
                $pid = (string)$child->UUpid;// 产品 id
                $ret = $s->Dynamic_Price_And_Storage($account,$pid,$begintime,1,0,0,$aid);
                if($ret!=105 && $ret!=1065){
                    $obj = simplexml_load_string($ret);
                    $total_money += (string)$obj->Rec->UUsprice * (int)$child->UUtnum;
                }
            }
        }
    }
    if($to_pft){
        //票付通总账号加钱——2015年4月13日11:56:33林农说要添加，宝椿提供的代码
        //$s->PFT_Member_Fund_Modify(1, 0, $total_fee, $add, 0,null,0, $p_type,$ordern,$memo);
        // 支付给了票付通，购买者账户加钱
        $s->PFT_Member_Fund_Modify($aid, 0, $total_fee, $add, 0, null, 0, $p_type, $ordern,$memo);
    }


    // 产品多级销售分润
//    $str="select aids_money from uu_order_fx_details where orderid='$ordern' limit 1";
//    $GLOBALS['le']->query($str);
//    $lirun = $GLOBALS['le']->fetch_assoc();
    $aids_money = unserialize($orders['mainOrder']->UUaids_money);
    if ($aids_money){
        // 子票没有记录分润，获取子票价格
        $childMenoy = array();
        if(count($orders['childOrder']) && $orders['mainOrder']->UUp_type!='F'){
            foreach($orders['childOrder'] as $key=>$child){
                $aids = explode(',',(string)$child->UUaids);
                krsort($aids);
                $aids_price = explode(',',(string)$child->UUaids_price);
                krsort($aids_price);
                foreach($aids as $k=>$et){
                    if($k==0) break;// 最后一个不用操作了
                    $childMenoy[$et]+= $aids_price[$k-1] * (string)$child->UUtnum;
                }
            }
        }
        // 主订单的 aids
        $main_aids = explode(',',(string)$orders['mainOrder']->UUaids);
//        $aids_money = unserialize($lirun['aids_money']);
        $opID = 0;
        foreach($aids_money as $row){
            $fid=$row[0];
            $action = ($row[2]==0) ? $cut : $add;//0减少,1增加和接口相反
            $Mmoney=$row[3];
            if($action==0){ // 加钱
                $ks = array_search($fid,$main_aids);
                $ks = $ks+1;
                $Mmoney += isset($childMenoy[$main_aids[$ks]]) ? $childMenoy[$main_aids[$ks]]:0;
            }else{
                $Mmoney += isset($childMenoy[$fid]) ? $childMenoy[$fid]:0;
            }

            if($row[1]==0){//信用额度
                $dmode = 1;
                $res = $s->PFT_Member_Fund_Modify($fid,$opID,$Mmoney,$action,$dmode,$row[4],0,2,$ordern);
            }else{//资金
                $dmode = 0;
                $res = $s->PFT_Member_Fund_Modify($fid,$opID,$Mmoney,$action,$dmode,NULL,0,0,$ordern);
            }
        }
    }

    return $total_money;// 返回上级利润

}

/**
 * 获取订单总价格，以分为单位
 *
 * @param $orders 订单数据
 * @param string $id_type 记录的ID类型，TOC为散客订单，TOB为分销商订单
 * @return int|string
 */
function total_fee($orders, $id_type = 'TOC')
{
    $total_fee = 0;
//    $aid = (int)$orders['mainOrder']->UUaid;
//    $mid = (int)$orders['mainOrder']->UUmid;

    $id = $id_type = 'TOB' ? (int)$orders['mainOrder']->UUmid :
        (int)$orders['mainOrder']->UUaid;

    $total_fee = ((int)$orders['mainOrder']->UUtprice * (int)$orders['mainOrder']->UUtnum);
    $ordernum = (string)$orders['mainOrder']->UUordernum;
    $str = <<<SQL
insert pft_member_alipay set fid=$id,orderid='$ordernum',
total_fee='$total_fee',money='$total_fee',ali_account='',rectime=now(),dtype=0
SQL;
    $GLOBALS['le']->query($str);

    // 加上非套票的子票价格
    if(count($orders['childOrder']) && $orders['mainOrder']->UUp_type!='F'){
        foreach($orders['childOrder'] as $key=>$child){
            $tmp = ((string)$child->UUtprice * (string)$child->UUtnum);
            $total_fee += $tmp;
            $ordernum = (string)$child->UUordernum;
            $str = <<<SQL
insert pft_member_alipay set fid=$id,orderid='$ordernum',total_fee='$tmp',
money='$tmp',ali_account='',rectime=now(),dtype=0
SQL;
            $GLOBALS['le']->query($str);
        }
    }
    return $total_fee;
}

function write_logs($txt,$file='/var/www/html/alipay/new_log.txt'){
    $fp = fopen($file,"a");
    flock($fp, LOCK_EX);
    fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}